import random

from faker import Faker

fake = Faker(locale="zh-CN")

def func_yaml(data):
    #判断数据类型
    if isinstance(data, dict):
        for key ,value in data.items():
            if '${' and '}' in str(value):
                #获取${}里面的东西
                start = str(value).index('${')
                end = str(value).index('}')
                # print(start)
                # print(end)
                #左边取到start，右边取不到end
                func_name = str(value)[(start+2):end]
                if 'name' in value:
                # func_age = str(value)[(start+2):end]
                    data[key] = str(value)[0:start] + eval(func_name) + str(value)[(end+1) : len(str(value))]
                if 'age' in value:
                # func_age = str(value)[(start+2):end]
                    data[key] = str(value)[0:start] + str(eval(func_name))
                # if 'random' in value:
                #     data[key] = eval(value)
    return data

def random_name():
    return fake.name()
def age():
    return random.randint(10,20)

if __name__ == '__main__':
    data = {'name': '上海-${random_name()}-测试', 'age': '少女-${age()}', 'sex': '女'}
    print(func_yaml(data))